package com.mfc.education.service.edu.service.impl;


import com.mfc.education.service.edu.entity.Video;
import com.mfc.education.service.edu.mapper.VideoMapper;
import com.mfc.education.service.edu.service.VideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mfc.education.service.edu.entity.Video;
import com.mfc.education.service.edu.feign.VodMediaService;
import com.mfc.education.service.edu.mapper.VideoMapper;
import com.mfc.education.service.edu.service.VideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author Mfc
 * @since 2021-07-25
 */
@Service
@Slf4j
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {



    @Autowired
    private VodMediaService vodMediaService;

    @Override
    public void removeMediaById(String id) {
        Video video = this.getById(id);
        if (video != null && StringUtils.isEmpty(video.getVideoSourceId())) {
            vodMediaService.removeVideo(video.getVideoSourceId());
        }
    }

    @Override
    public void removeMediaByChapterId(String chapterId) {
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("video_source_id");
        queryWrapper.eq("chapter_id", chapterId);
        List<Map<String, Object>> maps = getBaseMapper().selectMaps(queryWrapper);
        List<String> videoSourceIdList = this.getVideoSourceId(maps);

        vodMediaService.removeVideoByIdList(videoSourceIdList);
    }

    //     @Override
//    public void removeMediasByChapterId(String id) {
//        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("chapter_id", id);
//        List<Map<String, Object>> maps = videoMapper.selectMaps(queryWrapper);
//        ArrayList<String> idlist = new ArrayList<>();
//        for (Map<String, Object> map : maps) {
//            String sourceId = (String) map.get("video_source_id");
//            idlist.add(sourceId);
//        }
////        List<Video> videos = videoMapper.selectList(queryWrapper);
////        List<String> videoIds = videos.stream().map(Video::getVideoSourceId).collect(Collectors.toList());
//
//        ossVodService.removeVideoByIdList(idlist);
//    }


    @Override
    public void removeMediaByCourserId(String courserId) {
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("video_source_id");
        queryWrapper.eq("course_id", courserId);
        List<Map<String, Object>> maps = getBaseMapper().selectMaps(queryWrapper);
        List<String> videoSourceIdList = this.getVideoSourceId(maps);


        vodMediaService.removeVideoByIdList(videoSourceIdList);
    }



    private List<String> getVideoSourceId(List<Map<String, Object>> maps) {
        ArrayList<String> idList = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            String sourceId = (String) map.get("video_source_id");
            log.info(sourceId);
            if (StringUtils.isEmpty(sourceId)) continue;
            idList.add(sourceId);
        }

        return idList;
    }



}
